<?php

/**
 * Tanaxia_Core.php
 *
 * The Tanaxia_Core is the core and heart of this framework. It lets us load and access core objects, models, helpers, libraries,
 * plugins, autoload models, libraries and helpers
 * 
 * Copyright 2009, Marcus Schumann - Tanax
 * Licensed under The MIT License - Modification, Redistribution allowed but must retain the above copyright notice
 * @link 		http://www.opensource.org/licenses/mit-license.php
 *
 * @package 	Tanaxia_Core - TAF
 * @created 	Mars 26th 2009
 * @version 	1.0
 * @link		-
 * 
 * @changelog
 * - 09/03/26	v1.0	Initial release
 */

class Tanaxia_Core
{

	private static $coreClasses = array();
	private static $objects = array();
	
	public static function core($class, $ref = 'none')
	{
	
		if(is_array($class))
		{
		
			foreach($class as $a => $b)
			{
			
				if(is_numeric($a) && $b)
				{
				
					include(SYS_CORE . $b . EXT);
					self::$coreClasses[$b] = new $b();
					return self::$coreClasses[$b];
				
				}
				
				elseif(!is_numeric($a) && $b)
				{
				
					include(SYS_CORE . $a . EXT);
					self::$coreClasses[$b] = new $a();
					return self::$coreClasses[$b];
				
				}
				
				elseif($a && $b == null)
				{
				
					include(SYS_CORE . $a . EXT);
				
				}
			
			}
		
		}
		
		else
		{
		
			include(SYS_CORE . $class . EXT);
			
			if($ref != null)
			{
			
				$reference = ($ref == 'none') ? $class : $ref;
				self::$coreClasses[$reference] = new $class();
				
				return self::$coreClasses[$reference];
			
			}
		
		}
	
	}
	
	public static function get($class = null)
	{
		
		if(isset(self::$coreClasses[$class]))
		{
		
			return self::$coreClasses[$class];
		
		}
		
		elseif(isset(self::$objects[$class]))
		{
		
			return self::$objects[$class];
		
		}
		
		return false;
	
	}
	
	public static function autoload()
	{
		
		include(CONFIG . 'autoload' . EXT);
		
		foreach($autoload['libraries'] as $class => $reference)
		{
			
			// If no reference name has been set, class will be a numeric key
			$classname = (is_numeric($class)) ? $reference : $class;
			$name = ($classname == $reference) ? null : $reference;		
			
			
			self::loadLibrary($classname, $name);
			
		}
		
		foreach($autoload['models'] as $class => $reference)
		{
			
			// If no reference name has been set, class will be a numeric key
			$classname = (is_numeric($class)) ? $reference : $class;
			$name = ($classname == $reference) ? null : $reference;
			
			self::loadModel($classname, $name);
		
		}
		
		foreach($autoload['plugins'] as $class => $reference)
		{
			
			// If no reference name has been set, class will be a numeric key
			$classname = (is_numeric($class)) ? $reference : $class;
			$name = ($classname == $reference) ? null : $reference;
			
			self::loadPlugin($classname, $name);
		
		}
		
		foreach($autoload['helpers'] as $helper)
		{
			
			self::loadHelper($helper);
			
		}
	
	}
	
	public static function POT($ref = null)
	{
	
		include(POT . 'OTS' . EXT);
		
		$reference = ($ref == null) ? 'pot' : $ref;
		self::$coreClasses[$reference] = POT::getInstance();
	
	}
	
	public static function loadLibrary($lib, $ref = null, $param = null)
	{
	
		return self::load('library', $lib, $ref, $param);
	
	}
	
	public static function loadModel($model, $ref = null)
	{
	
		return self::load('model', $model, $ref);
	
	}
	
	public static function loadPlugin($plugin, $ref = null, $param = null)
	{
	
		return self::load('plugin', $plugin, $ref, $param);
	
	}
	
	public static function loadHelper($helper)
	{
	
		$filename = strtolower($helper) . '_helper';
		
		$sysHelpers = SYS_HELPERS . $filename . EXT;
		$appHelpers = HELPERS . $filename . EXT;
		
		if(file_exists($sysHelpers))
		{
			
			include($sysHelpers);
			return true;
			
		}
		
		elseif(file_exists($appHelpers))
		{
		
			include($appHelpers);
			return true;
		
		}
		
		die('Could not include helper: ' . $filename);
		return false;
	
	}
	
	/*
	|--------------------------------------------------------------------------
	| load
	|--------------------------------------------------------------------------
	|
	| Includes, instanciates and saves the reference to the class in an array
	| so we can access it later
	|
	*/
	private static function load($type, $classname, $reference = null, $param = null)
	{
		
		switch($type)
		{
			
			case 'library':
				$sysLibs = SYS_LIBS . $classname . EXT;
				$appLibs = LIBS . $classname . EXT;
				
				if(file_exists($sysLibs)) $pathToFile = $sysLibs;
				elseif(file_exists($appLibs)) $pathToFile = $appLibs;
				else $pathToFile = $sysLibs;
				
				$reference = ($reference == null) ? $classname : $reference;
				break;
				
			case 'model':
				$pathToFile = MODELS . $classname . EXT;
				$reference = ($reference == null) ? $classname : $reference;
				$classname = ucfirst($classname) . 'Model';
				break;
				
			case 'plugin':
				$sysPlugins = SYS_PLUGINS . $classname . EXT;
				$appPlugins = PLUGINS . $classname . EXT;
				
				if(file_exists($sysPlugins)) $pathToFile = $sysPlugins;
				elseif(file_exists($appPlugins)) $pathToFile = $appPlugins;
				else $pathToFile = $sysPlugins;
				
				$reference = ($reference == null) ? $classname : $reference;
				$classname = ucfirst($classname) . 'Plugin';
				break;
			
		}
		
		if(isset(self::$objects[$reference]))
		{
			
			return self::$objects[$reference];
			
		}
		
		if(class_exists($classname))
		{
			
			self::$objects[$reference] = ($param == null) ? new $classname() : new $classname($param);
			return self::$objects[$reference];
			
		}
		
		if(file_exists($pathToFile))
		{
			
			include($pathToFile);
			self::$objects[$reference] = ($param == null) ? new $classname() : new $classname($param);
			return self::$objects[$reference];
			
		}
		
		die('Cannot find the class: ' . $classname . ' at ' . $pathToFile);
		
	}
	
}


// End of Tanaxia_Core.php